﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CodeIn.Core;
using CodeIn.Core.Modules;

namespace CodeIn.Modules.Core {
	public class Commands : BaseModule {
		protected override void InitializeOverride() {
		}

		protected override bool CanExecuteOverride(string[] args) {
			switch (args[0]) {
				case "cd":
					if (args.Length != 2)
						return False("Usage is 'cd directory'");
					if (!Directory.Exists(args[1]))
						return False("Directory was not found");
					break;
			}
			return true;
		}

		protected override void ExecuteOverride(string[] args) {
			switch (args[0]) {
				case "pwd": Execute_pwd(args); break;
				case "ls": Execute_ls(args); break;
				case "dir": Execute_ls(args); break;
				case "cd": Execute_cd(args); break;
			}
		}

		// --------------------------------------------------------------------
		// Commands
		// --------------------------------------------------------------------

		/// <summary>
		/// PWD logic
		/// </summary>
		private void Execute_pwd(string[] args) {
			string path = OptionManager.Instance.GetValueFromName(Options.OPT_ENV_WORKINGDIRECTORY);
			LocalTrace.WriteLine(path);
		}

		/// <summary>
		/// LS logic
		/// </summary>
		private void Execute_ls(string[] args) {
			string path = OptionManager.Instance.GetValueFromName(Options.OPT_ENV_WORKINGDIRECTORY);
			string[] dirs = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
			string[] files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);

			// Initializes definitions
			string separator = "    ";
			int minSizeLength = 0;
			int maxSizeLength = -1;

			// Computes maxFileLength
			foreach (string file in files) {
				long size = new FileInfo(file).Length / 1024;
				maxSizeLength = Math.Max(size.ToString().Length, maxSizeLength);
			}
			maxSizeLength = Math.Max(minSizeLength, maxSizeLength);

			// Writes directories
			foreach (string dir in dirs) {
				var info = new DirectoryInfo(dir);
				string sizeStr = new string(' ', maxSizeLength);
				string dateStr = GetDateRepresentation(info.LastWriteTime);
				LocalTrace.WriteLine(dateStr + "  " + sizeStr + separator + "\\" + Path.GetFileName(dir));
			}

			// Writes files
			foreach (string file in files) {
				var info = new FileInfo(file);
				long size = info.Length / 1024;
				int sizeLength = size.ToString().Length;
				string sizeStr = new string(' ', maxSizeLength - sizeLength) + size;
				string dateStr = GetDateRepresentation(info.LastWriteTime);
				LocalTrace.WriteLine(dateStr + "  " + sizeStr + separator + Path.GetFileName(file));
			}
		}

		/// <summary>
		/// CD logic
		/// </summary>
		private void Execute_cd(string[] args) {
			var opt = OptionManager.Instance.GetOptionFromName(Options.OPT_ENV_WORKINGDIRECTORY);
			opt.Value = args[1];
			LocalTrace.WriteLine("Current directory is now : " + opt.Value);
		}

		// --------------------------------------------------------------------
		// HELPERS
		// --------------------------------------------------------------------

		private string GetDateRepresentation(DateTime date) {
			string ret = string.Format("{0:00}/{1:00}/{2:0000}  {3:00}:{4:00}", 
				date.Day, date.Month, date.Year, date.Hour, date.Minute);
			return ret;
		}
	}
}
